package com.gzhryc.shared_device.oem.h5.mng.controllers.place;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.place.services.dto.PlacePhysicalStoreSearch;
import com.gzhryc.shared_device.oem.code.statistics.dao.db.PlaceDayStatistics;
import com.gzhryc.shared_device.oem.code.statistics.dao.models.PlaceSumMoneyData;
import com.gzhryc.shared_device.oem.code.statistics.services.PlaceDayStatisticsService;
import com.gzhryc.shared_device.oem.code.statistics.services.dao.PlaceDayStatisticsSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PlaceStatisticsController {

    static Logger log = Logger.getLogger(PlaceStatisticsController.class);

    @Route(value = { "/place/statistics/index.html" })
    public String yearList(HttpServletRequest request) {
        String searchDate = DateFormatUtils.format(new Date(),"yyyy");
        request.setAttribute("searchDate",searchDate);
        return "/place/statistics/index.html";
    }

    @Route(value = { "/place/statistics/month/index.html" })
    public String monthList(HttpServletRequest request) {
        String searchDate = DateFormatUtils.format(new Date(),"yyyy-MM");
        request.setAttribute("searchDate",searchDate);
        return "/place/statistics/month.html";
    }

    @Route(value = { "/place/statistics/day/index.html" })
    public String list(HttpServletRequest request) {
        String searchDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd");
        request.setAttribute("searchDate",searchDate);
        return "/place/statistics/day.html";
    }

    @Route(value = "/place/statistics/year/list.data")
    public ListResult<PlaceSumMoneyData> yearListData(HttpServletRequest request) {
        ListResult<PlaceSumMoneyData> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        PlacePhysicalStoreSearch search = WebUtils.getParameter(request, PlacePhysicalStoreSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        String searchDate = WebUtils.getString(request, "searchDate",DateFormatUtils.format(new Date(),"yyyy"));
        try {
            Integer yearNum = Integer.valueOf(searchDate);

            Date createDate = DateUtils.parseDate(searchDate,"yyyy");

            List<PlacePhysicalStore> physicalStoreList = PlacePhysicalStoreService.self().findJoinBySearch(search,pageNo,pageLimit);
            result.getBody().setTotal(PlacePhysicalStoreService.self().countJoinBySearch(search));
            if (physicalStoreList != null && physicalStoreList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(new ArrayList<>());

                List<Long> placeIds = new ArrayList<>();
                for(PlacePhysicalStore place : physicalStoreList){
                    placeIds.add(place.getId());
                }

                List<PlaceSumMoneyData> placeSumMoneyDataList = PlaceDayStatisticsService.self(createDate).sumGroupPlaceIdByYear(yearNum,placeIds);
                for(PlacePhysicalStore place : physicalStoreList){
                    boolean flag = true;
                    if(placeSumMoneyDataList != null && placeSumMoneyDataList.size() > 0){
                        for(PlaceSumMoneyData sumMoneyData : placeSumMoneyDataList){
                            if(sumMoneyData.getPlaceId().equals(place.getId())){
                                sumMoneyData.setPlaceName(place.getName());
                                sumMoneyData.setPlaceAreaName(place.getAreaName());
                                sumMoneyData.setDayDate(searchDate);
                                result.getBody().getData().add(sumMoneyData);
                                flag = false;
                                break;
                            }
                        }
                    }

                    if(flag){
                        PlaceSumMoneyData sumMoneyData = new PlaceSumMoneyData();
                        sumMoneyData.setPlaceId(place.getId());
                        sumMoneyData.setPlaceName(place.getName());
                        sumMoneyData.setPlaceAreaName(place.getAreaName());
                        sumMoneyData.setDayDate(searchDate);
                        result.getBody().getData().add(sumMoneyData);
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    @Route(value = "/place/statistics/month/list.data")
    public ListResult<PlaceSumMoneyData> monthListData(HttpServletRequest request) {
        ListResult<PlaceSumMoneyData> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        PlacePhysicalStoreSearch search = WebUtils.getParameter(request, PlacePhysicalStoreSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        String searchDate = WebUtils.getString(request, "searchDate",DateFormatUtils.format(new Date(),"yyyy-MM"));
        try {
            String[] dateTemp = searchDate.split("-");
            Integer yearNum = Integer.valueOf(dateTemp[0]);
            Integer monthNum = Integer.valueOf(dateTemp[1]);

            Date createDate = DateUtils.parseDate(searchDate,"yyyy-MM");

            List<PlacePhysicalStore> physicalStoreList = PlacePhysicalStoreService.self().findJoinBySearch(search,pageNo,pageLimit);
            result.getBody().setTotal(PlacePhysicalStoreService.self().countJoinBySearch(search));
            if (physicalStoreList != null && physicalStoreList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(new ArrayList<>());

                List<Long> placeIds = new ArrayList<>();
                for(PlacePhysicalStore place : physicalStoreList){
                    placeIds.add(place.getId());
                }

                List<PlaceSumMoneyData> placeSumMoneyDataList = PlaceDayStatisticsService.self(createDate).sumGroupPlaceIdByYearAndMonth(yearNum,monthNum,placeIds);
                for(PlacePhysicalStore place : physicalStoreList){
                    boolean flag = true;
                    if(placeSumMoneyDataList != null && placeSumMoneyDataList.size() > 0){
                        for(PlaceSumMoneyData sumMoneyData : placeSumMoneyDataList){
                            if(sumMoneyData.getPlaceId().equals(place.getId())){
                                sumMoneyData.setPlaceName(place.getName());
                                sumMoneyData.setPlaceAreaName(place.getAreaName());
                                sumMoneyData.setDayDate(searchDate);
                                result.getBody().getData().add(sumMoneyData);
                                flag = false;
                                break;
                            }
                        }
                    }

                    if(flag){
                        PlaceSumMoneyData sumMoneyData = new PlaceSumMoneyData();
                        sumMoneyData.setPlaceId(place.getId());
                        sumMoneyData.setPlaceName(place.getName());
                        sumMoneyData.setPlaceAreaName(place.getAreaName());
                        sumMoneyData.setDayDate(searchDate);
                        result.getBody().getData().add(sumMoneyData);
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    @Route(value = "/place/statistics/day/list.data")
    public ListResult<PlaceDayStatistics> listData(HttpServletRequest request) {
        ListResult<PlaceDayStatistics> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        PlaceDayStatisticsSearch search = WebUtils.getParameter(request, PlaceDayStatisticsSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        String searchDate = WebUtils.getString(request, "searchDate");
        try {
            Date date = null;
            if (StringTools.isNotBlank(searchDate)) {
                date = DateUtils.parseDate(searchDate, "yyyy-MM-dd");
            } else {
                date = new Date();
            }
            String dayDate = DateFormatUtils.format(date, "yyyyMMdd");
            search.setDayDate(dayDate);

            result.getBody().setTotal(PlaceDayStatisticsService.self(date).countBySearch(search));
            List<PlaceDayStatistics> dayStatisticsList = PlaceDayStatisticsService.self(date).findBySearch(search, pageNo, pageLimit);
            if (dayStatisticsList != null && dayStatisticsList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(dayStatisticsList);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }
}
